package com.resou.reader.utils;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class MyGridItemDecoration extends RecyclerView.ItemDecoration {
    private int space;
    private int spanCount;

    /**
     * @param space     间隔的像素值
     * @param spanCount 列数
     */
    public MyGridItemDecoration(int space, int spanCount) {
        this.space = space;
        this.spanCount = spanCount;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left = space;
        outRect.bottom = space;
//        //网格布局的顶部那一行
        if (parent.getChildAdapterPosition(view) < spanCount) {
            outRect.top = space;
        }
//        //最后一列
        if (parent.getChildAdapterPosition(view) % spanCount == spanCount - 1) {
            outRect.right = space;
        }
    }
}
